﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Ezhu.WPFWin.Base;

namespace Ezhu.WPFWin
{
    /// <summary>
    /// TextWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TextWindow : Window
    {
        private static System.Windows.Forms.NotifyIcon notify;
        private HwndSource _hwndSource;//用来捕获win32窗体行为消息，利用Handle属性访问句柄
        public TextWindow()
        {
            InitializeComponent();
            this.SourceInitialized += delegate(object sender, EventArgs e)
            {
                //创建win32窗体
                this._hwndSource = PresentationSource.FromVisual((Visual) sender) as HwndSource;
                if (_hwndSource != null)
                {
                    _hwndSource.AddHook(ScreenManager.WndProc);
                }
            };
            NotifyIcon();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
        /// <summary>
        /// 窗口鼠标拉抻
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ResizePressed(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            string directionName = element.Name.Replace("Resize", "");
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                BaseWindow.ResizeWind(_hwndSource.Handle, directionName);
            }
        }
        private void grdMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //鼠标左键按下拖动窗口
            if (e.ButtonState == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
            //AppDomain.CurrentDomain.UnhandledException += delegate { ShutDown(); };
            //AppDomain.CurrentDomain.ProcessExit += delegate { ShutDown(); };
            //System.Threading.Thread.GetDomain().UnhandledException += delegate { ShutDown(); };
            //AppDomain.CurrentDomain.ProcessExit += delegate { ShutDown(); };
        }
        /// <summary>
        /// 双击最大化 双击还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdMain_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (WindowState == WindowState.Normal)
                {
                    this.WindowState = WindowState.Maximized;
                }
                else
                {
                    this.WindowState = WindowState.Normal;
                }
            }
        }
        /// <summary>
        /// 托盘图标初始化
        /// </summary>
        private void NotifyIcon()
        {
            notify = new System.Windows.Forms.NotifyIcon();
            notify.Icon = new System.Drawing.Icon("Favicon.ico");
            notify.Text = "播放器";
            notify.Visible = true;
            notify.MouseDown += new System.Windows.Forms.MouseEventHandler(Notify_Click);
        }

        private void Notify_Click(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                InitializationCMS();
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.Activate();
                notify.Visible = true;
                this.WindowState = WindowState.Normal;
                this.ShowInTaskbar = true;
            }
        }

        /// <summary>
        /// 初始化托盘的右键菜单
        /// </summary>
        private void InitializationCMS()
        {
            System.Windows.Forms.ContextMenuStrip cms = new System.Windows.Forms.ContextMenuStrip();
            System.Windows.Forms.ToolStripMenuItem itemOpenPlayer = new System.Windows.Forms.ToolStripMenuItem("打开播放器");
            System.Windows.Forms.ToolStripMenuItem itemOpenFile = new System.Windows.Forms.ToolStripMenuItem("打开媒体");
            System.Windows.Forms.ToolStripMenuItem itemExit = new System.Windows.Forms.ToolStripMenuItem("退出");
            System.Windows.Forms.ToolStripMenuItem itemSets = new System.Windows.Forms.ToolStripMenuItem("设置");

            System.Windows.Forms.ToolStripSeparator tssr = new System.Windows.Forms.ToolStripSeparator();
            System.Windows.Forms.ToolStripSeparator tssr1 = new System.Windows.Forms.ToolStripSeparator();
            //添加点击事件
            itemOpenPlayer.Click += new EventHandler(ItemOpenPlayer_Click);
            itemOpenFile.Click += new EventHandler(ItemOpenFile_Click);
            itemExit.Click += new EventHandler(ItemExit_Click);
            itemSets.Click += new EventHandler(ItemSets_Click);

            System.Windows.Forms.ImageList imglist = new System.Windows.Forms.ImageList();
            imglist.Images.Add(new System.Drawing.Icon("../../Image/Icon/player.ico"));
            itemOpenPlayer.Image = imglist.Images[0];
            cms.Items.Add(itemOpenPlayer);
            cms.Items.Add(itemOpenFile);
            cms.Items.Add(tssr);
            cms.Items.Add(itemSets);
            cms.Items.Add(tssr1);
            cms.Items.Add(itemExit);
            notify.ContextMenuStrip = cms;
        }

        /// <summary>
        /// 最小化
        /// </summary>
        private void SetBalloonTip()
        {
            notify.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
            notify.BalloonTipText = "***已最小化";
            notify.BalloonTipTitle = "提示...";
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMini_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
            this.ShowInTaskbar = false;
            SetBalloonTip();
            notify.ShowBalloonTip(1000);


        }
        private void ItemOpenPlayer_Click(object sender, EventArgs e)
        {
            //TODO SOME　CODE
        }

        private void ItemSets_Click(object sender, EventArgs e)
        {

        }


        private void ItemOpenFile_Click(object sender, EventArgs e)
        {

        }

        private void ItemExit_Click(object sender, EventArgs e)
        {
            RoutedEventArgs rea = new RoutedEventArgs();
            btnClose_Click(sender, rea);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (notify != null)
            {
                notify.Visible = false;
                notify.Dispose();
            }
            Application.Current.Shutdown();
        }
        /// <summary>
        /// 窗口最大化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMax_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
            {
                this.WindowState = WindowState.Maximized;
                btnMax.ToolTip = "还原窗口";
            }
            else
            {
                this.WindowState = WindowState.Normal;
                btnMax.ToolTip = "最大化";
            }
        }

        private void Canvas_Drop(object sender, DragEventArgs e)
        {

        }
        /// <summary>
        /// 左上角图标事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnIcon_Initialized(object sender, EventArgs e)
        {
            //右键菜单为空
            this.btnIcon.ContextMenu = null;
        }
        /// <summary>
        /// 左上角图标点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnIcon_Click(object sender, RoutedEventArgs e)
        {
            //目标
            this.contextMenu.PlacementTarget = this.btnIcon;
            //位置
            this.contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
            //显示菜单
            this.contextMenu.IsOpen = true;
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnStar_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnChooseFile_Click(object sender, RoutedEventArgs e)
        {

        }

        private void listBtn_Click(object sender, RoutedEventArgs e)
        {
            var i = this.Playlist.Width;
            if ((float)i.Value == 0)
            {
                this.Playlist.Width = new GridLength(0.25, GridUnitType.Star);
            }
            else
            {
                this.Playlist.Width = new GridLength(0);
            }

        }
    }
}
